package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.weiphone.com/ 威锋网
 *  abc
 */
public class WeiphoneNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.feng.com/.*/[0-9]{4}-[0-9]{2}-[0-9]{2}/.*[0-9]*.shtml",
//				"http://bbs.feng.com/read-htm-tid-[0-9]*.html",
				"http://www.feng.com/.*/.*_[0-9]+.shtml",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.contains("/game.")) {
				return false;
			}
			if (url.contains("/about/")) {
				return false;
			}
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title =doc.select("div.detail_title h1").text();
		if (StringUtils.isBlank(title)) {
			title =doc.select("h1").text();
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "_");
		}

		date=DateUtils.matchDate( doc.select("span.article_time").text());
		if (date == null) {
			date=DateUtils.matchDate( doc.select("div.authi>em>span").attr("title"));
		}
		if (date == null) {
			date=DateUtils.matchDate( doc.select("p.article_time").text());
		}
		if (date == null) {
			date=DateUtils.matchDate( doc.select("div.time").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(url);
		}

		content=HtmlCleaner.getContentHtml(url,doc.select("div.des_content"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.pcb"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article_main p"));
		}

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "div.ico user");
		if (StringUtils.isBlank(author)){
			author=doc.select("span.author").text();
		}

		Integer clickNum=NumberUtils.matchNumber(doc.select("span.view_count").text());
		Integer commentNum=NumberUtils.matchNumber(doc.select("span.cmt_count").text());
		news.setClickNum(clickNum);
		news.setCommentNum(commentNum);
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		/**
		 * 解析用于更新的地址
		 */

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		WeiphoneNewsAnalyse a=new WeiphoneNewsAnalyse();
		String url="http://www.feng.com/Story/Poking-fun-at-the-iPhone-battery-not-enough_632693.shtml";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
